package com.tuanzi.loan.business.repository.family.impl;

import com.tuanzi.loan.business.repository.family.custom.LoanNewCreditCardOrderRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.generated.jooq.loan.tables.LoanNewCreditCardOrder;
import com.tuanzi.loan.vo.request.family.LoanNewCreditCardOrderRequest;
import com.tuanzi.loan.vo.response.family.LoanNewCreditCardOrderResponse;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductChannel.COMM_PRODUCT_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanNewCreditCardOrder.LOAN_NEW_CREDIT_CARD_ORDER;

/**
 * @author caiwanlin
 * @create 2018-09-13 14:07
 */
public class LoanNewCreditCardOrderRepositoryImpl extends JOOQBaseRepositoryPlus implements LoanNewCreditCardOrderRepositoryCustom {
    @Override
    public Page<LoanNewCreditCardOrderResponse> list(LoanNewCreditCardOrderRequest searchRequest, Pageable pageable) {
        List<Integer> productIdList = searchRequest.getProductIdList();
        List<Integer> channelIdList = searchRequest.getChannelIdList();
        String platform= searchRequest.getPlatform();
        List<Integer> orderStatusList = searchRequest.getOrderStatusList();
        Date createStartTime = searchRequest.getCreateStartTime();
        Date createEndTime = searchRequest.getCreateEndTime();
        Date rebateStartTime = searchRequest.getRebateStartTime();
        Date rebateEndTime = searchRequest.getRebateEndTime();
        String mobnum = searchRequest.getMobnum();
        if (createEndTime != null){
            createEndTime = DateUtils.setHours(createEndTime,23);
            createEndTime = DateUtils.setMinutes(createEndTime,59);
            createEndTime = DateUtils.setSeconds(createEndTime,59);
        }
        if (rebateEndTime != null){
            rebateEndTime = DateUtils.setHours(rebateEndTime,23);
            rebateEndTime = DateUtils.setMinutes(rebateEndTime,59);
            rebateEndTime = DateUtils.setSeconds(rebateEndTime,59);
        }

        fields(LOAN_NEW_CREDIT_CARD_ORDER.CREATE_TIME,LOAN_NEW_CREDIT_CARD_ORDER.REBATE_TIME,
                LOAN_NEW_CREDIT_CARD_ORDER.PRDID,COMM_PRODUCT_PRDID.NAME,
                LOAN_NEW_CREDIT_CARD_ORDER.CHANNEL,COMM_PRODUCT_CHANNEL.CHANNEL_NAME,
                LOAN_NEW_CREDIT_CARD_ORDER.PLATFORM,LOAN_NEW_CREDIT_CARD_ORDER.MOBNUM,
                LOAN_NEW_CREDIT_CARD_ORDER.CARD_NAME,LOAN_NEW_CREDIT_CARD_ORDER.CARD_ID,
                LOAN_NEW_CREDIT_CARD_ORDER.REBATE_AMOUNT,LOAN_NEW_CREDIT_CARD_ORDER.ORDER_STATUS,
                LOAN_NEW_CREDIT_CARD_ORDER.USERNAME,LOAN_NEW_CREDIT_CARD_ORDER.IDCARD);
        leftJoin(COMM_PRODUCT_PRDID,COMM_PRODUCT_PRDID.PRDID.eq(LOAN_NEW_CREDIT_CARD_ORDER.PRDID));
        leftJoin(COMM_PRODUCT_CHANNEL,COMM_PRODUCT_CHANNEL.CHANNEL_CODE.eq(LOAN_NEW_CREDIT_CARD_ORDER.CHANNEL));
        in(LOAN_NEW_CREDIT_CARD_ORDER.PRDID,productIdList);
        in(LOAN_NEW_CREDIT_CARD_ORDER.CHANNEL,channelIdList);
        eq(LOAN_NEW_CREDIT_CARD_ORDER.PLATFORM,platform);
        in(LOAN_NEW_CREDIT_CARD_ORDER.ORDER_STATUS,orderStatusList);
        between(LOAN_NEW_CREDIT_CARD_ORDER.CREATE_TIME,createStartTime,createEndTime);
        between(LOAN_NEW_CREDIT_CARD_ORDER.REBATE_TIME,rebateStartTime,rebateEndTime);
        eq(LOAN_NEW_CREDIT_CARD_ORDER.MOBNUM,mobnum);
        orderBy(LOAN_NEW_CREDIT_CARD_ORDER.CREATE_TIME, Sort.Direction.DESC);
        Page<LoanNewCreditCardOrderResponse> search = search(pageable, record -> {
            LoanNewCreditCardOrderResponse response = new LoanNewCreditCardOrderResponse();
            response.setCreateTime(record.get(LOAN_NEW_CREDIT_CARD_ORDER.CREATE_TIME));
            response.setRebateTime(record.get(LOAN_NEW_CREDIT_CARD_ORDER.REBATE_TIME));
            response.setPrdId(record.get(LOAN_NEW_CREDIT_CARD_ORDER.PRDID));
            response.setProductName(record.get(COMM_PRODUCT_PRDID.NAME));
            response.setChannel(record.get(LOAN_NEW_CREDIT_CARD_ORDER.CHANNEL));
            response.setChannelName(record.get(COMM_PRODUCT_CHANNEL.CHANNEL_NAME));
            response.setPlatform(record.get(LOAN_NEW_CREDIT_CARD_ORDER.PLATFORM));
            response.setMobnum(PhoneNumNideUtil.hideNum(record.get(LOAN_NEW_CREDIT_CARD_ORDER.MOBNUM)));
            response.setCardName(record.get(LOAN_NEW_CREDIT_CARD_ORDER.CARD_NAME));
            response.setCardId(record.get(LOAN_NEW_CREDIT_CARD_ORDER.CARD_ID));
            response.setRebateAmount(record.get(LOAN_NEW_CREDIT_CARD_ORDER.REBATE_AMOUNT));
            response.setOrderStatus(record.get(LOAN_NEW_CREDIT_CARD_ORDER.ORDER_STATUS));
            response.setUsername(PhoneNumNideUtil.hideName(record.get(LOAN_NEW_CREDIT_CARD_ORDER.USERNAME)));
            response.setIdcard(PhoneNumNideUtil.hideIdCard(record.get(LOAN_NEW_CREDIT_CARD_ORDER.IDCARD)));
            return response;
        });


        return search;
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_NEW_CREDIT_CARD_ORDER;
    }
}
